package middleware

import (
	"blog_go/pkg/helper/jwt"
	"net/http"

	"github.com/gin-gonic/gin"
)

// 判断用户状态
func AuthStatusMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		v, exists := c.Get("claims")
		if !exists {
			c.JSON(http.StatusBadRequest, gin.H{
				"code":    2006,
				"message": "token错误",
				"data":    "{}",
			})
			c.Abort()
			return
		}

		status := v.(*jwt.MyClaims).Status //获取用户状态

		userType := v.(*jwt.MyClaims).UserType //获取用户类型

		//用户被禁用后无法编辑
		if c.Request.Method == "POST" && userType == 1 && status == 1 {
			c.JSON(http.StatusForbidden, gin.H{
				"code":    2007,
				"message": "被禁用,请联系管理员, 被解封后需重新登陆",
				"data":    "{}",
			})
			c.Abort()
			return
		}
	}
}
